﻿namespace SourceCodeRepository.Web.Controllers.Snippets
{
    using System.Web.Mvc;
    using Model.Entitites;
    using Persistence.Repositories.Interfaces;
    using ViewDataTransformers;
    using Views.ViewData;

    /// <summary>
    /// Controller class for the Create Snippet View
    /// </summary>
    public class CreateSnippetController : BaseController
    {
        /// <summary>
        /// Gets or sets the code snippet repository.
        /// </summary>
        /// <value>The code snippet repository.</value>
        protected ICodeSnippetRepository CodeSnippetRepository { get; set; }

        /// <summary>
        /// Gets or sets the contributor repository.
        /// </summary>
        /// <value>The contributor repository.</value>
        protected IContributorRepository ContributorRepository { get; set; }

        /// <summary>
        /// Gets or sets the language repository.
        /// </summary>
        /// <value>The language repository.</value>
        protected ILanguageRepository LanguageRepository { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="CreateSnippetController"/> class.
        /// </summary>
        /// <param name="codeSnippetRepository">The code snippet repository.</param>
        /// <param name="languageRepository">The language repository.</param>
        /// <param name="contributorRepository">The contributor repository.</param>
        public CreateSnippetController(ICodeSnippetRepository codeSnippetRepository, 
                                       ILanguageRepository languageRepository, IContributorRepository contributorRepository)
        {
            this.CodeSnippetRepository = codeSnippetRepository;
            this.LanguageRepository = languageRepository;
            this.ContributorRepository = contributorRepository;
        }

        /// <summary>
        /// Creates a code snippet.
        /// </summary>
        /// <returns></returns>
        public ActionResult CreateSnippet()
        {
            int contributorId = int.Parse(Request.Form["ContributorId"]);
            int languageId = int.Parse(Request.Form["LanguageId"]);
            string snippetText = Request.Form["SnippetText"];

            CodeSnippet snippet = new CodeSnippet
                                      {
                                          Contributor = this.ContributorRepository.GetById(contributorId),
                                          Language = this.LanguageRepository.GetById(languageId),
                                          SnippetText = snippetText
                                      };

            this.CodeSnippetRepository.Save(snippet);
            return View();
        }

        /// <summary>
        /// Shows the view.
        /// </summary>
        /// <returns></returns>
        public override ActionResult ShowView()
        {
            var viewData = new CreateSnippetViewData
                               {
                                   Contributors =
                                       new ContributorViewDataTransformer().Transform(this.ContributorRepository.GetAllEntities()),
                                   Languages =
                                       new LanguageViewDataTransformer().Transform(this.LanguageRepository.GetAllEntities())
                               };
            return View("Snippets/CreateSnippet",viewData);
        }
    }
}